package cn.itcast.controller;

/*
 *Author:TH
 *Dete:2019/10/2019:08
 */

import cn.itcast.constant.MessageConstant;
import cn.itcast.entity.PageResult;
import cn.itcast.entity.QueryPageBean;
import cn.itcast.entity.Result;
import cn.itcast.pojo.CheckGroup;
import cn.itcast.service.CheckGroupService;
import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/checkgroup")
@SuppressWarnings("all")
public class CheckGroupsController {

    @Reference
    private CheckGroupService checkGroupService;

    //添加检查项
    @RequestMapping("/add")
    @PreAuthorize("hasAuthority('CHECKGROUP_ADD')")//权限校验
    public Result add(@RequestBody CheckGroup checkGroup,Integer[] checkitemIds){
        System.out.println("*************************");
        //用try catch来判断是否执行成功
        try{
            checkGroupService.add(checkGroup,checkitemIds);

        }catch (Exception e){
            //添加失败
            return new Result(false,MessageConstant.ADD_CHECKGROUP_FAIL);
        }
        //添加成功,返回消息
        return new Result(true, MessageConstant.ADD_CHECKGROUP_SUCCESS);
    }

    //分页查询
    @RequestMapping("/findByPage")
    @PreAuthorize("hasAuthority('CHECKGROUP_QUERY')")//权限校验
    public PageResult findByPage(@RequestBody QueryPageBean queryPageBean){
        Integer cp = queryPageBean.getCurrentPage();
        Integer ps = queryPageBean.getPageSize();
        String qs = queryPageBean.getQueryString();
        PageResult pageResult = checkGroupService.pageQuery(cp,ps,qs);

        return pageResult;
    }

    //删除检查项
    @RequestMapping("/delectById")
    @PreAuthorize("hasAuthority('CHECKGROUP_DELETE')")//权限校验
    public Result delectById(Integer id){
        try {
            checkGroupService.delectById(id);
        }catch(RuntimeException r){
            //删除失败
            return new Result(false,r.getMessage());
        }catch (Exception e){
            //操作失败
            return new Result(false,MessageConstant.DELETE_CHECKITEM_FAIL);
        }
        return new Result(true,MessageConstant.DELETE_CHECKITEM_SUCCESS);
    }

    //按照id查询检查组
    @RequestMapping("/findById")
    @PreAuthorize("hasAuthority('CHECKGROUP_QUERY')")//权限校验
    public Result findById(Integer id){
        try {
            CheckGroup checkGroup = checkGroupService.findById(id);
            return new Result(true,MessageConstant.QUERY_CHECKGROUP_SUCCESS,checkGroup);
        }catch (Exception e){
            return new Result(false,MessageConstant.QUERY_CHECKGROUP_FAIL);
        }
    }

    //按照id查询检查项
    @RequestMapping("/findCheckItemIdsByCheckGroupId")
    @PreAuthorize("hasAuthority('CHECKGROUP_QUERY')")//权限校验
    public Result findCheckItemIdsByCheckGroupId(Integer id){
        try {
            List<Integer> checkitemIds= checkGroupService.findCheckItemIdsByCheckGroupId(id);
            return new Result(true,MessageConstant.QUERY_CHECKITEM_SUCCESS,checkitemIds);
        }catch (Exception e){
            return new Result(false,MessageConstant.QUERY_CHECKITEM_FAIL);
        }
    }

    //按照id查询检查项
    @RequestMapping("/edit")
    @PreAuthorize("hasAuthority('CHECKGROUP_EDIT')")//权限校验
    public Result edit(@RequestBody CheckGroup checkGroup,Integer[] checkitemIds){
        try {
            checkGroupService.edit(checkGroup,checkitemIds);
            return new Result(true,MessageConstant.EDIT_CHECKGROUP_SUCCESS);
        }catch (Exception e){
            return new Result(false,MessageConstant.EDIT_CHECKGROUP_FAIL);
        }
    }

    //查找全部
    @RequestMapping("findAll")
    @PreAuthorize("hasAuthority('CHECKGROUP_QUERY')")//权限校验
    public Result findAll(){

        List<CheckGroup> checkItemList =checkGroupService.findAll();

        if (checkItemList!=null&&checkItemList.size()>0){
            //查询成功
            return new Result(true,MessageConstant.QUERY_CHECKITEM_SUCCESS,checkItemList);
        }else{
            //查询失败
            return new Result(false,MessageConstant.QUERY_CHECKITEM_FAIL);
        }
    }

}
